﻿/*  CamHelper (See http://camhelper.codeplex.com/ or https://bitbucket.org/AndiKS/camhelper for more Info)
    Copyright (C) 2011 Andreas Wimmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.IO;
using SystemWrapper;
using SystemWrapper.IO;

namespace CamHelper.Engine
{
    public class TopSolidFile : ITopSolidFile
    {

        private IFileWrap _file;
        private string _fullNameLower;

        public TopSolidFileType FileType { get; private set; }
        public string FullName { get; private set; }
        public virtual bool IsDocument { get; private set; }
        public bool IsEmpty { get; private set; }
        public bool IsSpezial { get; private set; }
        public bool IsStandardComponent { get; private set; }
        public string Name { get; private set; }
        public string Directory { get; private set; }
        public bool RscExists { get; private set; }
        public IDateTimeWrap LastSave { get; private set; }

        #region Constructor

        public TopSolidFile(string path, IFileWrap file)
        {
            _file = file;
            IsEmpty = String.IsNullOrEmpty(path);

            if (IsEmpty)
            {
                FullName = String.Empty;
                _fullNameLower = String.Empty;
                Name = String.Empty;
                Directory = String.Empty;
                FileType = TopSolidFileType.Empty;
                LastSave = new DateTimeWrap(DateTime.MinValue);
            }
            else
            {
                FullName = path;
                _fullNameLower = path.ToLower();
                Name = Path.GetFileName(path);
                Directory = Path.GetDirectoryName(path);
                LastSave = new DateTimeWrap(DateTime.MinValue);

                #region detect FileType

                string ext = Path.GetExtension(_fullNameLower);
                switch (ext)
                {
                    case ".cam":
                        FileType = TopSolidFileType.Cam;
                        break;

                    case ".top":
                        FileType = TopSolidFileType.Top;
                        break;

                    case ".rsc":
                        if (_fullNameLower.EndsWith(".cam.rsc"))
                            FileType = TopSolidFileType.CamRsc;
                        else
                            FileType = TopSolidFileType.TopRsc;
                        break;

                    case ".dft":
                        FileType = TopSolidFileType.Dft;
                        break;

                    default:
                        FileType = TopSolidFileType.Empty;
                        break;
                }

                #endregion

                IsStandardComponent = _fullNameLower.StartsWith(@"$std=");
                IsSpezial = _fullNameLower.StartsWith("$");

                Update();
            }
        }

        #endregion

        public void Update()
        {
            if (!IsSpezial)
            {
                RscExists = _file.Exists(FullName + ".rsc");
                LastSave = _file.GetLastWriteTime(FullName);
            }
        }

    }
}
